package com.atguigu.yygh.hosp.controller;


import cn.hutool.crypto.SecureUtil;
import com.atguigu.yygh.common.result.R;
import com.atguigu.yygh.hosp.service.HospitalSetService;
import com.atguigu.yygh.model.hosp.HospitalSet;
import com.atguigu.yygh.vo.hosp.HospitalSetQueryVo;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 * 医院设置表 前端控制器
 * </p>
 *
 * @author atguigu
 * @since 2021-08-27
 */
@Api(tags = "医院设置")
@RestController
@RequestMapping("/admin/hosp/hospitalSet")
public class HospitalSetController {

    private final HospitalSetService hospitalSetService;

    public HospitalSetController(HospitalSetService hospitalSetService) {
        this.hospitalSetService = hospitalSetService;
    }

    @ApiOperation("查询列表")
    @GetMapping("findAll")
    public R findAll() {
        return R.ok().data(hospitalSetService.list());

    }

    @ApiOperation("删除 By ID")
    @DeleteMapping("{id}")
    public R removeById(@ApiParam(name = "id", value = "医院设置主键", required = true) @PathVariable Long id) {

//        try {
//            int a = 10 / 0;
//        } catch (Exception e) {
//            throw new YyghException(1000, "lllll");
//        }

        boolean b = hospitalSetService.removeById(id);
        if (b) {
            return R.ok();
        } else {
            return R.error();
        }
    }

    @ApiOperation(value = "查询列表 By 分页")
    @GetMapping("{current}/{size}")
    public R pageList(@ApiParam(name = "current", value = "当前页码", required = true) @PathVariable Long current,
                      @ApiParam(name = "size", value = "每页记录数", required = true) @PathVariable Long size) {

        Page<HospitalSet> hospitalSetPage = new Page<>(current, size);
        Page<HospitalSet> page = hospitalSetService.page(hospitalSetPage);
        return R.ok().data(page.getTotal(), page.getRecords());
    }

    @ApiOperation(value = "查询列表 By 分页+条件")
    @PostMapping("{current}/{size}")
    public R pageQuery(@ApiParam(name = "current", value = "当前页码", required = true) @PathVariable Long current,
                       @ApiParam(name = "size", value = "每页记录数", required = true) @PathVariable Long size,
                       @ApiParam(name = "hospitalSetQueryVo", value = "查询对象")
                       @RequestBody(required = false) HospitalSetQueryVo hospitalSetQueryVo) {

        Page<HospitalSet> page = hospitalSetService.pageQueryList(current, size, hospitalSetQueryVo);
        return R.ok().data(page.getTotal(), page.getRecords());
    }

    @ApiOperation(value = "新增")
    @PostMapping("saveHospSet")
    public R save(
            @ApiParam(name = "hospitalSet", value = "医院设置对象", required = true)
            @RequestBody HospitalSet hospitalSet) {

        boolean b = hospitalSetService.saveHospSet(hospitalSet);

        return b ? R.ok() : R.error();
    }

    @ApiOperation(value = "查询 By ID")
    @GetMapping("getHospSet/{id}")
    public R getById(
            @ApiParam(name = "id", value = "医院设置ID", required = true) @PathVariable String id) {
        return R.ok().data("item", hospitalSetService.getById(id));
    }

    @ApiOperation(value = "修改 By ID")
    @PutMapping("updateHospSet")
    public R updateById(
            @ApiParam(name = "hospitalSet", value = "医院设置对象", required = true)
            @RequestBody HospitalSet hospitalSet) {
        boolean b = hospitalSetService.updateById(hospitalSet);
        return b ? R.ok() : R.error();
    }

    @ApiOperation(value = "批量删除")
    @DeleteMapping("batchRemove")
    public R batchRemoveHospitalSet(@RequestBody List<Long> idList) {
        // 即使 id 不存在，也不会报错
        return hospitalSetService.removeByIds(idList) ? R.ok() : R.error();
    }

    @ApiOperation(value = "锁定和解锁")
    @PutMapping("lockHospitalSet/{id}/{status}")
    public R lockHospitalSet(@PathVariable Long id,
                             @PathVariable Integer status) {
        // 根据 id 查询医院设置
        HospitalSet hospitalSet = hospitalSetService.getById(id);
        // 修改状态(1=锁定，0=正常)
        hospitalSet.setStatus(status);
        return hospitalSetService.updateById(hospitalSet) ? R.ok() : R.error();
    }

    @ApiOperation(value = "根据医院编号获取医院签名")
    @PutMapping("getSignKey/{hoscode}")
    public String getSignKey(@PathVariable String hoscode) {
        return SecureUtil.md5(hospitalSetService.getSignKey(hoscode));
    }

    @ApiOperation(value = "根据医院编号获取接口回调地址")
    @PutMapping("getApiUrl/{hoscode}")
    public String getApiUrl(@PathVariable String hoscode) {
        return hospitalSetService.getApiUrl(hoscode);
    }
}

